package com.thwj.project.app.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thwj.common.exception.ServiceException;
import com.thwj.framework.security.LoginUser;
import com.thwj.project.app.api.vo.AppUserVipVO;
import com.thwj.project.biz.domain.User;
import com.thwj.project.biz.domain.UserVip;
import com.thwj.project.biz.enums.UserVipStatusEnum;
import com.thwj.project.biz.enums.WxUserAuthSourceTypeEnum;
import com.thwj.project.biz.service.IUserService;
import com.thwj.project.biz.service.IUserVipService;
import com.thwj.project.pay.OrderNumberUtil;
import com.thwj.project.pay.WxPayManger;
import com.thwj.project.pay.domain.WxPay;
import com.thwj.project.pay.dto.PayResultDTO;
import com.thwj.project.pay.dto.WxPayJSAPIDto;
import com.thwj.project.pay.enums.OrderSourceTypeEnum;
import com.thwj.project.pay.enums.PayStatusEnum;
import com.thwj.project.pay.service.IWxPayService;
import com.thwj.project.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Slf4j
@Transactional
@Service
public class AppUserVipService {

    @Autowired
    IUserVipService userVipService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IWxPayService wxPayService;
    @Autowired
    private IUserService userService;
    @Autowired
    WxPayManger wxPayManger;


    /***
     * 获取用户 vip 信息
     * @param loginUser
     * @return
     */
    public AppUserVipVO getUserVip(LoginUser loginUser) {

        String configOrderAmount = configService.selectConfigByKey("app.vip.amount");
        BigDecimal orderAmount = new BigDecimal(configOrderAmount);

        UserVip userVip = userVipService.getOne(
                new QueryWrapper<UserVip>()
                        .eq("user_id", loginUser.getUserId())
                        .orderByDesc("create_date"),false);
        if(userVip == null) {
            userVip = new UserVip();
            userVip.setUserId(loginUser.getUserId());
            userVip.setCreateDate(new Date());
            userVip.setAmount(orderAmount);
            userVip.setStatus(UserVipStatusEnum.waiting_valid.getValue());
            userVipService.save(userVip);
        }
        return getAppUserVipVO(userVip);
    }

    private AppUserVipVO getAppUserVipVO(UserVip userVip) {
        AppUserVipVO appUserVipVO = new AppUserVipVO();
        appUserVipVO.setVipStatus(userVip.getStatus());
        appUserVipVO.setAmount(userVip.getAmount());
        return appUserVipVO;
    }

    /***
     * 获取用户 vip 信息
     * @param loginUser
     * @return
     */
    public PayResultDTO createVipOrder(LoginUser loginUser) {
        User user = userService.getById(loginUser.getUserId());

        //  创建VIP订单
        UserVip userVip = userVipService.getOne(
                new QueryWrapper<UserVip>()
                        .eq("user_id", loginUser.getUserId())
                        .orderByDesc("create_date"),false);

        if(UserVipStatusEnum.working.getValue() == userVip.getStatus()) {
            throw new ServiceException("会员生效中，无需重复支付");
        }
        WxPay wxPay = initOrderPay(userVip);
        wxPayService.save(wxPay);

        userVip.setOrderNumber(wxPay.getOrderNumber());
        userVipService.updateById(userVip);

        // 创建微信小程序单
        WxPayJSAPIDto wxPayJSAPIDto = new WxPayJSAPIDto();
        wxPayJSAPIDto.setOpenId(user.getMiniOpenid());
        wxPayJSAPIDto.setSourceTypeEnum(WxUserAuthSourceTypeEnum.MINI_PROGRAM_OPENID);
        wxPayJSAPIDto.setOrderNum(wxPay.getOrderNumber());
        wxPayJSAPIDto.setOrderDesc("购买会员");
        wxPayJSAPIDto.setOrderMoney(wxPay.getActualMoney());
        PayResultDTO payResultDTO = wxPayManger.createWxJsapiTradeInfo(wxPayJSAPIDto);

        return payResultDTO;
    }

    private WxPay initOrderPay(UserVip userVip) {
        WxPay wxPay = new WxPay();
        wxPay.setCreateDate(new Date());
        wxPay.setStatus(PayStatusEnum.WAITING_PAY.getValue());
        wxPay.setUserId(userVip.getUserId());
        wxPay.setActualMoney(userVip.getAmount());
        wxPay.setTotalMoney(userVip.getAmount());
        wxPay.setOrderNumber(OrderNumberUtil.getOrderNumberBySourceType(OrderSourceTypeEnum.VIP_ORDER));
        wxPay.setSourceId(userVip.getId());
        wxPay.setSourceType(OrderSourceTypeEnum.VIP_ORDER.getValue());
        return wxPay;
    }



}
